package com.or.common.utils;

import static android.Manifest.permission.READ_EXTERNAL_STORAGE;
import static android.Manifest.permission.READ_MEDIA_IMAGES;
import static android.Manifest.permission.READ_MEDIA_VIDEO;
import static android.Manifest.permission.READ_MEDIA_VISUAL_USER_SELECTED;
import static android.content.pm.PackageManager.PERMISSION_GRANTED;

import android.Manifest;
import android.app.Activity;
import android.content.Context;
import android.os.Build;

import androidx.activity.ComponentActivity;
import androidx.activity.result.ActivityResultLauncher;
import androidx.activity.result.contract.ActivityResultContracts;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;

public class CMPermissionUtils {

    public static final int PERMISSIONS_REQUEST_READ_EXTERNAL_STORAGE = 1;

    public static boolean hasReadExternalStoragePermission(Context context) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU &&
                (ContextCompat.checkSelfPermission(context, READ_MEDIA_IMAGES) == PERMISSION_GRANTED ||
                        ContextCompat.checkSelfPermission(context, READ_MEDIA_VIDEO) == PERMISSION_GRANTED)) {
            // Full access on Android 13 (API level 33) or higher
            return true;
        } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.UPSIDE_DOWN_CAKE && ContextCompat.checkSelfPermission(context, READ_MEDIA_VISUAL_USER_SELECTED) == PERMISSION_GRANTED) {
            // Partial access on Android 14 (API level 34) or higher
            return true;
        } else if (ContextCompat.checkSelfPermission(context, READ_EXTERNAL_STORAGE) == PERMISSION_GRANTED) {
            // Full access up to Android 12 (API level 32)
            return true;
        } else {
            // Access denied
            return false;
        }
    }

    public static void requestReadExternalStoragePermission(Activity activity) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
            // 请求 READ_MEDIA_IMAGES 和 READ_MEDIA_VIDEO 权限
            ActivityCompat.requestPermissions(activity, new String[]{READ_MEDIA_IMAGES, READ_MEDIA_VIDEO, Manifest.permission.READ_MEDIA_AUDIO}, PERMISSIONS_REQUEST_READ_EXTERNAL_STORAGE);
        } else {
            // 对于 Android 14 以下版本，继续使用旧的权限
            ActivityCompat.requestPermissions(activity, new String[]{READ_EXTERNAL_STORAGE}, PERMISSIONS_REQUEST_READ_EXTERNAL_STORAGE);
        }
    }

    public static String[] getReadExternalStoragePermissions() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.UPSIDE_DOWN_CAKE) {
            return new String[]{
                    Manifest.permission.READ_MEDIA_IMAGES,
                    Manifest.permission.READ_MEDIA_VIDEO,
                    Manifest.permission.READ_MEDIA_VISUAL_USER_SELECTED
            };
        } else if (Build.VERSION.SDK_INT == Build.VERSION_CODES.TIRAMISU) {
            return new String[]{
                    Manifest.permission.READ_MEDIA_IMAGES,
                    Manifest.permission.READ_MEDIA_VIDEO
            };
        } else {
            return new String[]{
                    Manifest.permission.READ_EXTERNAL_STORAGE
            };
        }
    }

    public static ActivityResultLauncher<String[]> registerForActivityResultMedia(ComponentActivity activity, OnPermissionsCallback callback) {
        return registerForActivityResult(activity, getReadExternalStoragePermissions(), callback);
    }

    public static ActivityResultLauncher<String[]> registerForActivityResult(ComponentActivity activity, String[] permissions, OnPermissionsCallback callback) {
        return activity.registerForActivityResult(new ActivityResultContracts.RequestMultiplePermissions(), result -> {
            boolean allGranted = true;
            for (Boolean granted : result.values()) {
                if (!granted) {
                    allGranted = false;
                    break;
                }
            }
            if (allGranted) {
                // 所有请求的权限都被用户授予
                // 可以执行相关操作
                callback.onPermissionsGranted();
            } else {
                // 至少有一个权限未被用户授予
                callback.onPermissionsDenied();
            }
        });
    }

    public interface OnPermissionsCallback {
        void onPermissionsGranted();

        void onPermissionsDenied();
    }

}
